package liangtWorkSpace.threadDemo.thread;

/**
 * @author liangt
 * @ClassName: Match4
 * @title: Match4
 * @projectName liangtWorkSpace.threadDemo.thread
 * @description: 模仿死锁
 * @date 2020/11/3 12:12
 */
public class Match4 {
    public static String fileA = "文件A";
    public static String fileB = "文件B";

    public static void main(String[] args) {
        while (true){
            new Thread(){
                public void run(){
                    synchronized (fileA){
                        System.out.println(Thread.currentThread().getName()+":读取"+fileA);
                        synchronized (fileB){
                            System.out.println(Thread.currentThread().getName()+":读取"+fileB);
                        }
                        System.out.println(Thread.currentThread().getName()+":文件："+fileB + ":" + fileA+"保存完成");
                    }
                }
            }.start();

            new Thread(){
                public void run(){
                    synchronized (fileB){
                        System.out.println(Thread.currentThread().getName()+":读取"+fileB);
                        synchronized (fileA){
                            System.out.println(Thread.currentThread().getName()+":读取"+fileA);
                        }
                        System.out.println(Thread.currentThread().getName()+":文件："+fileB + ":" + fileA+"保存完成");
                    }
                }
            }.start();
        }
    }
}
